
import './app.css'
import './pages/index/index'
import {GetCart} from './api/api.js'
import {get} from './utils/util.js'
import { render, WeElement, define } from 'omi'

define('my-app', class extends WeElement {

  config = {
    pages: [
      'pages/index/index',
      'pages/mine/index',
      'pages/discover/index/index',
      'pages/discover/scenicDetail/index',
      'pages/shopping_mall/index/index',
      'pages/shopping_mall/shopping_details/details',
      'pages/create_order/index',
      'pages/food/index/index',
      'pages/food/detail/index',
      'pages/return/index/index',
      'pages/return/return_apply/index',
      'pages/return/my_shipping_address/index',
      'pages/return/add_shipping_address/index',
      'pages/invoice/index',
      'pages/guest/index',
      'pages/collect/index',
      'pages/pay_success/index',
      'pages/outhtml/outhtml'
    ],
    
    subpackages:[
      {
        "name": "my",
        "root": "pages/my/",
        "pages": [
          'coupon/index',
          'points/index',
          'about_us/index',
          'my_invitation/index',
          'my_youhui/index',
          'my_card/index',
          'my_cart/index',
          'my_order/index',]
      },
      {
        "name": "scenic",
        "root": "pages/scenic/",
        "pages": [
          'list/index',
          'detail/index',
          'inner/index',
        ]
      }
    ],
    window: {
      backgroundTextStyle: 'light',
      navigationBarBackgroundColor: '#fff',
      navigationBarTitleText: 'WeChat',
      navigationBarTextStyle: 'black'
    },
    "tabBar":{
      "selectedColor": "#02b5db",
      "list":[
        {
          "pagePath": "pages/index/index",
          "iconPath": "images/icon1.png",
          "selectedIconPath": "images/zy-icon11.png",
          "text":"首页"
        },
        {
        "pagePath": "pages/discover/index/index",
        "iconPath": "images/zy-icon2.png",
        "selectedIconPath": "images/zy-icon22.png",
        "text": "发现"
        },
        {
          "pagePath": "pages/shopping_mall/index/index",
          "iconPath": "images/zy-icon3.png",
          "selectedIconPath": "images/zy-icon33.png",
          "text": "商城"
        },
        {
          "pagePath": "pages/mine/index",
          "iconPath": "images/zy-icon4.png",
          "selectedIconPath": "images/zy-icon44.png",
          "text": "我的"
        },
      ]
    },
    "permission": {
      "scope.userLocation": {
        "desc": "你的位置信息将用于小程序位置接口的效果展示"
      }
    },
  }

  globalData = {
    userInfo: null,
    cart:[],
    select_address: null,
    idmunbe:null
  }

  install() { 
    // 展示本地存储能力
    var access_token = wx.getStorageSync('access_token') 
    var userInfo = wx.getStorageSync('userInfo') 

    this.globalData.access_token = access_token;
    this.globalData.userInfo = userInfo;

    if(access_token){
      get(GetCart).then((res) => {
        this.globalData.cart = res.data;
      })
      // // 登录
      // wx.login({
      //   success: res => {
      //     // 发送 res.code 到后台换取 openId, sessionKey, unionId
      //     console.log('login',res);
          
      //   }
      // })
    }
    if(!userInfo){
      // 获取用户信息
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            console.log('this',this);
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            wx.getUserInfo({
              success: res => {
                console.log('getUserInfo',res);
                
                // 可以将 res 发送给后台解码出 unionId
                this.globalData.userInfo = res.userInfo
                wx.setStorage({
                  key: 'userInfo',
                  data: res.userInfo,
                  success: (result)=>{
                    console.log('userInfo save')
                  },
                });
                // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                // 所以此处加入 callback 以防止这种情况
                if (this.userInfoReadyCallback) {
                  this.userInfoReadyCallback(res)
                }
                
              }
            })
          }
        }
      })      
    }


  }

  onShow() { }

  onHide() { }

  render() {
    return (
      <page-my />
    )
  }
})


render(<my-app />, '#app')
